local cjson = require "cjson"
local jwt = require "resty.jwt"

---get params
---local args = ngx.req.get_uri_args()

---post params
ngx.req.read_body()
local args = ngx.req.get_post_args()

---user password judge
if args.u ~= ngx.var.jwt_userId or args.p ~= ngx.var.jwt_userPassword then
	ngx.say(' user is not valid!');
    	ngx.status = ngx.HTTP_UNAUTHORIZED
    	ngx.exit(ngx.HTTP_OK)
end

----生成的jwt格式
----{"iss":"zhijl_web","exp":1536573108,"userId":"11","identityFlag":"629b5b4e-f187-454f-9168-a2fb29e346a2"}	
local jwt_token = jwt:sign(
		ngx.var.jwt_secret,
		{
		header={typ="JWT", alg="HS256"},
		payload={iss=ngx.var.jwt_iss,
			userId=ngx.var.jwt_userId,
			identityFlag="testnow",
			exp=os.time()+1000*60*30}
		}	
)

ngx.header['Set-Cookie'] = "jwt=" .. jwt_token .."; path=/"
ngx.var.jwt_token=jwt_token
--ngx.say('token is '..jwt_token)
